﻿using System.Configuration;
using System.IO;
using System.Reflection;
using DevExpress.Mvvm;
using DevExpress.Utils;
using DevExpress.Xpf.DemoBase.Helpers;
using DevExpress.Xpf.DemoBase.Helpers.TextColorizer;
using Microsoft.Win32;
using orm.Models;

namespace OneStress.ViewModels
{
    public class ScenarioViewModel : PanelWorkspaceViewModel
    {
        public ScenarioViewModel() {
            IsClosed = false;
        }
        public ScenarioViewModel(string displayName, string text) : this() {
            DisplayName = displayName;
            CodeLanguageText = new CodeLanguageText(CodeLanguage.CS, text);
        }

        public CodeLanguage CodeLanguage { get; private set; }
        public CodeLanguageText CodeLanguageText { get; private set; }
        public string Description { get; protected set; }
        public string FilePath { get; protected set; }
        public string Footer { get; protected set; }
        protected override string WorkspaceName { get { return "DocumentHost"; } }

        public bool OpenFile() {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "OneSys Files (*.proj)|*.proj";
            openFileDialog.FilterIndex = 1;
            bool? dialogResult = openFileDialog.ShowDialog();
            bool dialogResultOK = dialogResult.HasValue && dialogResult.Value;
            if(dialogResultOK) {
                DisplayName = openFileDialog.SafeFileName;
                FilePath = openFileDialog.FileName;
                var fp = $"{Path.GetDirectoryName(FilePath)}\\数据文件\\ProjectDB.wdb";

                var con = ConfigurationManager.ConnectionStrings["Sqlite"];

                ServiceContainer.Default.RegisterService("DBSerice", new OneSysDBContext(con.ConnectionString.Replace("*", fp)));
            }
            return dialogResultOK;
        }
        public override void OpenItemByPath(string path) {
            DisplayName = Path.GetFileName(path);
            FilePath = path;
            SetCodeLanguageProperties(Path.GetExtension(path));
            CodeLanguageText = new CodeLanguageText(CodeLanguage, () => { return GetCodeTextByPath(path); });
            IsActive = true;
        }
        string GenerateClassText(string className) {
            string text = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VisualStudioDocking {{
    class {0} {{
    }}
}}";
            return string.Format(text, className);
        }
        CodeLanguage GetCodeLanguage(string fileExtension) {
            switch(fileExtension) {
                case ".cs": return CodeLanguage.CS;
                case ".vb": return CodeLanguage.VB;
                case ".xaml": return CodeLanguage.XAML;
                default: return CodeLanguage.Plain;
            }
        }
        string GetCodeTextByPath(string path) {
            Assembly assembly = typeof(ScenarioViewModel).Assembly;
            using(Stream stream = AssemblyHelper.GetResourceStream(assembly, path, true)) {
                if(stream == null)
                    return GenerateClassText(Path.GetFileNameWithoutExtension(path));
                using(StreamReader reader = new StreamReader(stream))
                    return reader.ReadToEnd();
            }
        }
        string GetDescription(CodeLanguage codeLanguage) {
            switch(codeLanguage) {
                case CodeLanguage.CS: return "Visual C# Source file";
                case CodeLanguage.VB: return "Visual Basic Source file";
                case CodeLanguage.XAML: return "Windows Markup File";
                default: return "Other file";
            }
        }
        void SetCodeLanguageProperties(string fileExtension) {
            CodeLanguage = GetCodeLanguage(fileExtension);
            Description = GetDescription(CodeLanguage);
            Footer = DisplayName;
            Glyph = CodeLanguage == CodeLanguage.XAML ? Images.FileXaml : CodeLanguage == CodeLanguage.CS ? Images.Project : null;
        }
    }
}
